
    GetName = function(var) deparse(substitute(var))
    GetVar  = function(varname)  eval(as.symbol(varname))
    
    logit = function(p) log(p/(1-p))
    expit = function(x){
        output = rep(1,length(x))
        index  = !is.na(x) & x <= 100
        e.x    = exp(x[index])
        output[index] = e.x/(1+e.x)
        output[is.na(x)] = NA
        return(output)
    } 
    
    Mean = function(x) mean(x, na.rm=TRUE)

    NameVar = function(list,name){
        
        varname1 = strsplit(name,split=".",fixed=TRUE)[[1]][1]
        paste(varname1,"=",list[name][[1]],sep="")
        
    }
    
    NameList = function(x){
        
        sapply(1:length(x), function(i) NameVar(x,names(x)[i]))
        
    }
    
    NameFirst = function(names){
        
        sapply(names,  function(name) strsplit(name,split=".",fixed=TRUE)[[1]][1])
        
    }
    
    ParaValues = function(paras){
        
        ###     Create an array containing all the parameter value combinations
        para.string = paras[[1]]
        for(i in 2:length(paras))   para.string = outer(para.string,paras[[i]],"paste")
        
        ###     Extract numerical values from strings
        Extract = function(name) as.numeric(strsplit(name,split=" ",fixed=TRUE)[[1]])
        para.value = apply(para.string,1:length(dim(para.string)),Extract)          # The first dimension contains the results
        dimnames(para.value)[[1]] = NameFirst(names(paras))
        dim.order  = c(2:length(dim(para.value)),1)
        return(aperm(para.value, dim.order))
        
    }
    
    MyAttach = function(list){  # attach a list to the global environment
        for(i in 1:length(names(list))){
            assign(names(list)[i], list[[i]], envir=.GlobalEnv)
        }
    }
    
    
    Message = function(paras, para, ptm1){
        para.min = sum(sapply(paras[1:(length(para)-1)], min))
        output   = NULL
        if(sum(para[1:(length(para)-1)]) == para.min){
            if(para["n"] > min(paras$n.cand)){
                output = paste(output, " takes ",round((proc.time() - ptm1)[3],
                                                       3),"s \n",sep = "")  
                ptm1 <<- proc.time()
            }
            output = paste(output, "Sample size ", para["n"], sep = "")
        }
        return(output)
    }
    
    # Print = function()
    
   
    
    
    ##### Rounding functions (start) #####
    
    RoundS = function(x,num){
        #' Round a number by scientific digits
        #'
        #' Args:
        #'     x: The number to be rounded
        #'     num: the number of scientific digits
        #' Returns:
        #'      The rounded number
        #'
        gsub("\\.$", "", formatC(signif(x,digits=num),digits=num,format="fg", flag="#"))
    }
    
    
    
    RoundD = function(x,num){
        #' Round a number by decimal points
        sprintf(paste("%.",num,"f",sep=""), round(x,num))
    }
    
    Round = function(x, num){
        # Round large numbers by scientific digits
        # Round small number by decimal places
        
        output = RoundS(x,num)
        output[x<1 & !is.na(x)] = RoundD(x[x<1 & !is.na(x)],num)
        if(is.array(x)) output = array(output, dim=dim(x), dimnames=dimnames(x))
        return(output)
    }
    
    ##### Rounding functions (end) #####
    
    
    
    